package iwpl.project.servlet;

import iwpl.project.dao.ApartmentsDAO;
import iwpl.project.dao.AreasDAO;
import iwpl.project.entities.Apartments;
import iwpl.project.entities.Areas;
import iwpl.project.entities.Contracts;
import iwpl.project.entities.Departments;
import iwpl.project.entities.Employees;
import iwpl.project.entitymanagerhelper.EntityManagerHelper;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import iwpl.project.dao.*;
public class Control_Contract extends HttpServlet {
	
	public static String ACTION_DELETE = "delete";
	public static String ACTION_INSERTBUYING = "insertbuying";
	public static String ACTION_INSERTHIRING = "inserthiring";
	public static String ACTION_UPDATEBUYING = "updatebuying";
	public static String ACTION_UPDATEHIRING = "updatehiring";
	//public static String ACTION_UPDATE="update";
	
	/**
	 * Constructor of the object.
	 */
	public Control_Contract() {
		super();
	}
	public static void ExecuteControl(String Control, Contracts contr)
	{
		ContractsDAO dao=new ContractsDAO();
		if(Control=="SaveBuying")
		{
			EntityManagerHelper.beginTransaction();
			dao.save(contr);
			EntityManagerHelper.commit();
		}
		if(Control=="SaveHiring")
		{
			EntityManagerHelper.beginTransaction();
			dao.save(contr);
			EntityManagerHelper.commit();
		}
		if(Control=="UpdateBuying")
		{
			
			EntityManagerHelper.beginTransaction();
			dao.update(contr);
			EntityManagerHelper.commit();
		}
		if(Control=="UpdateHiring")
		{
			
			EntityManagerHelper.beginTransaction();
			dao.update(contr);
			EntityManagerHelper.commit();
		}
		if(Control=="Delete")
		{
			
			EntityManagerHelper.beginTransaction();
			dao.delete(contr);
			EntityManagerHelper.commit();
		}
		
	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			doProcess(request, response);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			doProcess(request, response);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	public void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, ParseException
	{
		String sID=request.getParameter("ctrid");
		Contracts contr=new Contracts();
		//String statusinsert=request.getParameter("bnt_insert");
		//String statusupdate=request.getParameter("bnt_update");
		
		String action = request.getParameter("action");
		
		if(null != action && ACTION_DELETE.equals(action))
		{			
			contr.setId(Integer.parseInt(request.getParameter("id")));
			ExecuteControl("Delete", contr);
			response.sendRedirect("/DepartmentManage/Manage_Contract.jsp");
			
			return;
		}
		if("ajax".equals(action))
		{
			PrintWriter out=response.getWriter();
			/*PrintWriter out=response.getWriter();
			int deid=Integer.parseInt(action);
			DepartmentsDAO dao=new DepartmentsDAO();
			Departments ar=dao.findById(deid);
			Areas data=new Areas();
			Collection<Areas> getar=ar.getAreases();
			Iterator ir=getar.iterator();
			out.print("<select name=cboarea>");
			while(ir.hasNext())
			{
				data=(Areas)ir.next();
				int areaid=data.getId();
				String areaname=data.getAreaName();
				out.print("<option value="+areaid+">"+areaname+"</option>");
			}
			out.print("</select>");*/
			String id=request.getParameter("id");
			out.print("alert("+id+")");
		}
		if(null!=action && ACTION_INSERTBUYING.equals(action))
		{
			String signingdate=request.getParameter("txtsigningdate");
			String purchasedate=request.getParameter("txtpurchasedate");
			String receivedate=request.getParameter("txtreceivedate");
			SimpleDateFormat dateformat=new SimpleDateFormat("dd-MM-yyyy");
			Integer contracttype=1;
			Integer empl=Integer.parseInt(request.getParameter("cboemp"));
			//Integer apt=Integer.parseInt(request.getParameter("hdapartment"));
			Date dsigningdate=dateformat.parse(signingdate);
			Date dpurchasedate=dateformat.parse(purchasedate);
			Date dreceivedate=dateformat.parse(receivedate);
			Timestamp time1=new Timestamp(dsigningdate.getTime());
			Timestamp time2=new Timestamp(dpurchasedate.getTime());
			Timestamp time3=new Timestamp(dreceivedate.getTime());
			EmployeesDAO edao=new EmployeesDAO();
			ApartmentsDAO adao=new ApartmentsDAO();
			Employees sempl=edao.findById(empl);
			//Apartments sapart=adao.findById(apt);
			contr.setContractType(contracttype);
			contr.setEmployees(sempl);
			//contr.setApartments(sapart);
			contr.setSigningDate(time1);
			contr.setPurchaseDate(time2);
			contr.setReceivedOfDate(time3);
			ExecuteControl("SaveBuying", contr);
			response.sendRedirect("/DepartmentManage/Manage_Contract.jsp");
		}
		if(null!=action && ACTION_INSERTHIRING.equals(action))
		{
			String signingdate=request.getParameter("txtsigningdate");
			String hiringdate=request.getParameter("txthiringdate");
			String enddate=request.getParameter("txtenddate");
			SimpleDateFormat dateformat=new SimpleDateFormat("dd-MM-yyyy");
			Integer contracttype=2;
			Integer empl=Integer.parseInt(request.getParameter("cboemp"));
			//Integer apt=Integer.parseInt(request.getParameter("hdapartment"));
			Date dsigningdate=dateformat.parse(signingdate);
			Date dhiringdate=dateformat.parse(hiringdate);
			Date denddate=dateformat.parse(enddate);
			Timestamp time1=new Timestamp(dsigningdate.getTime());
			Timestamp time2=new Timestamp(dhiringdate.getTime());
			Timestamp time3=new Timestamp(denddate.getTime());
			EmployeesDAO edao=new EmployeesDAO();
			ApartmentsDAO adao=new ApartmentsDAO();
			Employees sempl=edao.findById(empl);
			//Apartments sapart=adao.findById(apt);
			contr.setContractType(contracttype);
			contr.setEmployees(sempl);
			//contr.setApartments(sapart);
			contr.setSigningDate(time1);
			contr.setHiringDate(time2);
			contr.setEndDate(time3);
			ExecuteControl("SavingHiring", contr);
			response.sendRedirect("/DepartmentManage/Manage_Contract.jsp");
		}
		if(null!=action && ACTION_UPDATEBUYING.equals(action))
		{
			String signingdate=request.getParameter("txtsigningdate");
			String purchasedate=request.getParameter("txtpurchasedate");
			String receivedate=request.getParameter("txtreceivedate");
			SimpleDateFormat dateformat=new SimpleDateFormat("dd-MM-yyyy");
			Integer contracttype=1;
			Integer empl=Integer.parseInt(request.getParameter("cboemp"));
			//Integer apt=Integer.parseInt(request.getParameter("hdapartment"));
			Date dsigningdate=dateformat.parse(signingdate);
			Date dpurchasedate=dateformat.parse(purchasedate);
			Date dreceivedate=dateformat.parse(receivedate);
			Timestamp time1=new Timestamp(dsigningdate.getTime());
			Timestamp time2=new Timestamp(dpurchasedate.getTime());
			Timestamp time3=new Timestamp(dreceivedate.getTime());
			EmployeesDAO edao=new EmployeesDAO();
			ApartmentsDAO adao=new ApartmentsDAO();
			Employees sempl=edao.findById(empl);
			//Apartments sapart=adao.findById(apt);
			contr.setId(Integer.parseInt(sID));
			contr.setContractType(contracttype);
			contr.setEmployees(sempl);
			//contr.setApartments(sapart);
			contr.setSigningDate(time1);
			contr.setPurchaseDate(time2);
			contr.setReceivedOfDate(time3);
			ExecuteControl("UpdateBuying", contr);
			response.sendRedirect("/DepartmentManage/Manage_Contract.jsp");
		}
		if(null!=action && ACTION_UPDATEHIRING.equals(action))
		{
			String signingdate=request.getParameter("txtsigningdate");
			String hiringdate=request.getParameter("txthiringdate");
			String enddate=request.getParameter("txtenddate");
			SimpleDateFormat dateformat=new SimpleDateFormat("dd-MM-yyyy");
			Integer contracttype=2;
			Integer empl=Integer.parseInt(request.getParameter("cboemp"));
			//Integer apt=Integer.parseInt(request.getParameter("hdapartment"));
			Date dsigningdate=dateformat.parse(signingdate);
			Date dhiringdate=dateformat.parse(hiringdate);
			Date denddate=dateformat.parse(enddate);
			Timestamp time1=new Timestamp(dsigningdate.getTime());
			Timestamp time2=new Timestamp(dhiringdate.getTime());
			Timestamp time3=new Timestamp(denddate.getTime());
			EmployeesDAO edao=new EmployeesDAO();
			ApartmentsDAO adao=new ApartmentsDAO();
			Employees sempl=edao.findById(empl);
			//Apartments sapart=adao.findById(apt);
			contr.setId(Integer.parseInt(sID));
			contr.setContractType(contracttype);
			contr.setEmployees(sempl);
			//contr.setApartments(sapart);
			contr.setSigningDate(time1);
			contr.setHiringDate(time2);
			contr.setEndDate(time3);
			ExecuteControl("UpdateHiring", contr);
			response.sendRedirect("/DepartmentManage/Manage_Contract.jsp");
		}
		
		}
}
